package android.slc.module.sys.ui.dict.type

import android.slc.appbase.ui.dialog.SlcFastDialogUtils
import android.slc.appbase.ui.vd.BaseListPageViewVdByFx
import android.slc.appbase.ui.widget.table.TableViewUtils
import android.slc.appdb.sys.entity.CsDictType
import android.slc.box.commonlibrary.compat.SlcResourceUtils
import javafx.fxml.FXMLLoader
import javafx.scene.Node
import javafx.scene.control.Button
import javafx.scene.control.Label
import javafx.scene.control.TableCell
import javafx.scene.control.TableColumn

class DictTypePageVd : BaseListPageViewVdByFx<CsDictType, DictTypePageVmBox>() {

    var viewDictAction: ((CsDictType) -> Unit)? = null

    var editDictTypeAction: ((CsDictType) -> Unit)? = null

    lateinit var tcDictName: TableColumn<CsDictType, String>
    lateinit var tcDictType: TableColumn<CsDictType, String>
    lateinit var tcStatus: TableColumn<CsDictType, String>
    lateinit var tcSort: TableColumn<CsDictType, Int>
    lateinit var tcRemark: TableColumn<CsDictType, String>
    lateinit var tcAction: TableColumn<CsDictType, Long>

    override fun initSwipeRecycler() {
        super.initSwipeRecycler()
        tcDictName = tableView.columns[0] as TableColumn<CsDictType, String>
        tcDictName.setCellFactory { getTextItem() }
        tcDictType = tableView.columns[1] as TableColumn<CsDictType, String>
        tcDictType.setCellFactory { getTextItem() }
        tcStatus = tableView.columns[2] as TableColumn<CsDictType, String>
        tcStatus.setCellFactory { getTextItem() }
        tcSort = tableView.columns[3] as TableColumn<CsDictType, Int>
        tcSort.setCellFactory { getTextItem() }
        tcRemark = tableView.columns[4] as TableColumn<CsDictType, String>
        tcRemark.setCellFactory { getTextItem() }
        tcAction = tableView.columns[5] as TableColumn<CsDictType, Long>
        tcAction.setCellFactory { getActionItem() }
        TableViewUtils.setHeadLabelCenter(tableView, true)
    }

    private fun <T> getTextItem(): TableCell<CsDictType, T?> {
        return object : TableCell<CsDictType, T?>() {
            override fun updateItem(item: T?, empty: Boolean) {
                super.updateItem(item, empty)
                if (!empty) {
                    val itemView =
                        FXMLLoader.load<Node>(DictTypePageVd::class.java.getResource("DictTypeItemText.fxml"))
                    (itemView.lookup("#value") as Label).text = item?.toString()
                    graphic = itemView
                } else {
                    graphic = null
                }
            }
        }
    }

    private fun getActionItem(): TableCell<CsDictType, Long?> {
        return object : TableCell<CsDictType, Long?>() {
            override fun updateItem(item: Long?, empty: Boolean) {
                super.updateItem(item, empty)
                if (!empty) {
                    val itemView =
                        FXMLLoader.load<Node>(DictTypePageVd::class.java.getResource("DictTypeItemAction.fxml"))
                    (itemView.lookup("#btnViewDict") as Button).setOnAction {
                        viewDictAction?.invoke(tableView.items[index])
                    }
                    (itemView.lookup("#btnEdit") as Button).setOnAction {
                        editDictTypeAction?.invoke(tableView.items[index])
                    }
                    (itemView.lookup("#btnDelete") as Button).setOnAction {
                        deleteDialog(tableView.items[index])
                    }
                    graphic = itemView
                } else {
                    graphic = null
                }
            }
        }
    }


    private fun deleteDialog(csDictType: CsDictType) {
        SlcFastDialogUtils.createBuilder().setContentText(SlcResourceUtils.getString("label_delete_really")).build()
            .showAndWait().ifPresent { itButtonType ->
                if (SlcFastDialogUtils.btnIsOk(itButtonType)) {
                    vmBox.onDelete(csDictType)
                }
            }
    }
}